package transport

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"service/common/doc"
)

const (
	SuccessCode  = 0
	FatalErrCode = 100 + iota
	BindJsonErrCode
	BindParamErrCode
)

type Resp map[string]interface{}

func SuccessResp(c *gin.Context) {
	c.JSON(http.StatusOK, Resp{"success": "ok", "errorcode": SuccessCode})
}

func DataResp(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, Resp{"data": data, "errorcode": SuccessCode})
}

func ErrResp(c *gin.Context, code int, err error) {
	c.AbortWithStatusJSON(http.StatusOK, Resp{"errormsg": err.Error(), "errorcode": code})
}

func FatalResp(c *gin.Context, err error) {
	c.AbortWithStatusJSON(http.StatusOK, Resp{"errormsg": err.Error(), "errorcode": FatalErrCode})
}

func init() {
	suc := doc.ApiDoc{
		Code:        SuccessCode,
		Description: "成功",
	}
	fat := doc.ApiDoc{
		Code:        FatalErrCode,
		Description: "系统内部错误",
	}
	jso := doc.ApiDoc{
		Code:        BindJsonErrCode,
		Description: "json类型不匹配",
	}
	par := doc.ApiDoc{
		Code:        BindParamErrCode,
		Description: "部分参数丢失",
	}
	doc.GenDoc(suc, fat, jso, par)
}
